// pages/cart/cart.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    carts:[],
    selectAll:true,
    cartAmount:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var carts = wx.getStorageSync("carts");
    var cartAmount = wx.getStorageSync('cartAmount');
    this.setData({carts:carts,cartAmount:cartAmount}); 
  }, 

  // 删除商品
  removeGoods:function(e){
    var id = e.currentTarget.dataset.id;
    var carts = wx.getStorageSync("carts");
    for(var i in carts){
      console.log(carts[i].id);
      if(carts[i].id==id){
         carts.splice(i,1);
      }
    }
    this.setData({carts:carts,});
    wx.setStorageSync("carts", carts)
    this.calculateAmount();
  },

  //增加商品
  increaseGoods: function (e) {
    var id = e.currentTarget.dataset.id;
    var carts = wx.getStorageSync("carts");
    for (var i = 0; i < carts.length; i++) {
      if (carts[i].id == id) {
        carts[i].quantity = carts[i].quantity + 1;
        break;
      }
    }
    this.setData({ carts: carts });
    wx.setStorageSync("carts", carts);
    this.calculateAmount();
  },
  //减少商品
  decreaseGoods: function (e) {
    var id = e.currentTarget.dataset.id;
    var carts = wx.getStorageSync("carts");
    for(var i=0;i<carts.length;i++){
      if((carts[i].id==id)&&(carts[i].quantity>1)){
        carts[i].quantity=carts[i].quantity-1;
        break; 
      }
    }
    this.setData({ carts: carts });
    wx.setStorageSync("carts", carts);
    this.calculateAmount();
  },
  //选择商品
  changeSelect:function(e){
    var id = e.currentTarget.dataset.id;
    var carts = wx.getStorageSync("carts");
    for (var i = 0; i < carts.length; i++) {
      if (carts[i].id == id) {
        carts[i].selected = ! carts[i].selected;
        break;
      }
    }
    this.setData({ carts: carts });
    wx.setStorageSync("carts", carts);
    this.calculateAmount();
  },
  //全选
  selectAll:function(e){
    var selectAll =! this.data.selectAll;
    this.setData({selectAll:selectAll})
    var carts = wx.getStorageSync("carts");
    for (var i = 0; i < carts.length; i++) {
        carts[i].selected = selectAll;
    }
    this.setData({ carts: carts });
    wx.setStorageSync("carts", carts);
    this.calculateAmount();
  },
  //计算购物车金额
  calculateAmount(){
    var carts = wx.getStorageSync("carts");
    var cartAmount =0;
    for (var i = 0; i < carts.length; i++) {
      if(carts[i].selected){
          cartAmount = cartAmount+ carts[i].quantity*carts[i].memberPrice;
      }
    }
    this.setData({ cartAmount: cartAmount });
    wx.setStorageSync('cartAmount', cartAmount);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})